package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_decistiontypeFeign;
import com.power.sifa.approve.model.Apl_decistiontype;
import com.power.sifa.approve.model.pageInfo.Apl_decistiontypePageInfo;
import com.power.sifa.approve.service.Apl_decistiontypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   决定类型控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_decistiontypeController",tags={"决定类型"})
public class Apl_decistiontypeController implements IApl_decistiontypeFeign {

    @Resource
    private Apl_decistiontypeService apl_decistiontypeService;

    
    @Override
    @ApiOperation("查询决定类型分页列表")
    @SaCheckPermission("apl_decistiontype_list")
    public ResponseDTO list(Apl_decistiontypePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_decistiontype> page = this.apl_decistiontypeService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_decistiontype apl_decistiontype : page) {
                apl_decistiontype.geneText();
                list.add(apl_decistiontype);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看决定类型")
    @SaCheckPermission("apl_decistiontype_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_decistiontype apl_decistiontype  = apl_decistiontypeService.getById(id);
        if (apl_decistiontype != null) {
            apl_decistiontype.geneText();
        }
        return ResponseDTO.succData(apl_decistiontype);
    }

    @Override
    @ApiOperation("删除决定类型")
    @SaCheckPermission("apl_decistiontype_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_decistiontypeService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改决定类型")
    @SaCheckPermission("apl_decistiontype_save")
    public ResponseDTO save(Apl_decistiontype apl_decistiontype) {
        Assert.notNull(apl_decistiontype, "apl_decistiontype不能为空");
        apl_decistiontypeService.saveOrUpdate(apl_decistiontype);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除决定类型")
    @SaCheckPermission("apl_decistiontype_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_decistiontypeService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_decistiontype_list")
    public void output(HttpServletResponse response, Apl_decistiontypePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "决定类型";
        String[] heads = {"序号","父层级id","名称","层级","排序"};
        String[] fields = {"id","parentid","name","level","sort"};
        Page<Apl_decistiontype> page = this.apl_decistiontypeService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "决定类型";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","父层级id","名称","层级","排序"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","parentid","name","level","sort"};
        List<Apl_decistiontype> list = (List<Apl_decistiontype>)ExcelUtil.Import(fields, file, Apl_decistiontype.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_decistiontypeService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO getDecisionTypeTree() {
        return ResponseDTO.succData(this.apl_decistiontypeService.getDecisionTypeTree());
    }

}
